﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GTP.AppFrame.PrototypeTools.Common;

namespace GTP.AppFrame.PrototypeTools.Business.Page
{
    /// <summary>
    /// 编辑页
    /// </summary>
    public class GTPEditPage : GTPPage
    {
        /// <summary>
        /// 生成页面
        /// </summary>
        public override void Gennerator(BizProperty property)
        {
            string fileName = string.Empty;
            string data = string.Empty;
            fileName = string.Format("{0}EditPage.metadata.page", property.Name);
            data = GeneratorUtility.ReadConfigMetadata(GeneratorUtility.ConfigMetadataName.EditPage);
            data = property.Fill(data);
            data = data.Replace("{GTP-PT:PropertyList}", GetPropertyFieldList(property.MainEntityPropetyList));
            GeneratorUtility.WriteMetadata(fileName, data);
        }

        /// <summary>
        /// 处理属性，生成界面控件
        /// </summary>
        /// <param name="propertyList"></param>
        /// <returns></returns>
        private string GetPropertyFieldList(List<EntityProperty> propertyList)
        {
            string result = string.Empty;
            string fieldConfig = string.Empty;
            for (int i = 0; i < propertyList.Count; i++)
            {
                fieldConfig = GetPropertyFieldConfig(propertyList[i].Type);
                result += fieldConfig.Replace("{GTP-PT:ID}", propertyList[i].ID.ToString())
                    .Replace("{GTP-PT:Name}", propertyList[i].Name);
            }
            return result;
        }
        /// <summary>
        /// 根据属性类型获取属性的配置
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private string GetPropertyFieldConfig(EntityProperyType type)
        {
            Dictionary<EntityProperyType, string> typeDict = new Dictionary<EntityProperyType, string>();
            if (!typeDict.ContainsKey(type))
            {
                string fieldConfig = GeneratorUtility.ReadConfigEntityField(type);
                typeDict.Add(type, fieldConfig);
            }
            return typeDict[type];
        }
    }
}
